<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<el-row :gutter="20">
					<!-- 禁用字段区域（只读信息） -->
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name">
							<template #label>
								<WtmLabelTooltip label="简称" tooltip="物料分类的简短名称" />
							</template>
							<el-input v-model="stateData.vmModel.Entity.Name" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_Code_FormItem" prop="Entity.Code">
							<template #label>
								<WtmLabelTooltip label="编码" tooltip="用于系统内部识别的唯一编码" />
							</template>
							<el-input v-model="stateData.vmModel.Entity.Code" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_ErpId_FormItem" prop="Entity.ErpId">
							<template #label>
								<WtmLabelTooltip label="金蝶ERP编号" tooltip="与外部ERP系统同步时使用的编号" />
							</template>
							<el-input v-model="stateData.vmModel.Entity.ErpId" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item
							ref="Entity_Level_FormItem"
							prop="Entity.Level"
							label="层级"
							:rules="[{ required: true, message: '层级为必填项', trigger: 'blur' }]"
						>
							<el-input-number v-model="stateData.vmModel.Entity.Level" disabled class="width100"></el-input-number>
						</el-form-item>
					</el-col>

					<!-- 可编辑字段区域（详情页全部禁用） -->
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_State_FormItem" prop="Entity.State" :rules="[{ required: true, message: '状态为必填项', trigger: 'blur' }]">
							<template #label>
								<WtmLabelTooltip label="状态" tooltip="物料分类的当前状态（如启用、禁用等）" />
							</template>
							<el-select v-model="stateData.vmModel.Entity.State" disabled class="width100">
								<el-option key="启用" value="启用" label="启用"></el-option>
								<el-option key="禁用" value="禁用" label="禁用"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_ParentId_FormItem" prop="Entity.ParentId" label="父级">
							<el-select v-model="stateData.vmModel.Entity.ParentId" disabled class="width100">
								<el-option v-for="item in stateData.AllSysMaterialGroups" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="24" class="mb20">
						<el-form-item ref="Entity_Description_FormItem" prop="Entity.Description">
							<template #label>
								<WtmLabelTooltip label="详情" tooltip="用于存储该物料分类的详细说明信息" />
							</template>
							<el-input type="textarea" :rows="3" v-model="stateData.vmModel.Entity.Description" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="24" class="mb20">
						<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
							<template #label>
								<WtmLabelTooltip label="备注" tooltip="计量单位组的备注信息" />
							</template>
							<el-input type="textarea" :rows="3" v-model="stateData.vmModel.Entity.Remark" disabled></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysMaterialGroupApi as api } from '/@/views/supplychain/sysmaterialgroup/api';
import other from '/@/utils/other';
import { ISysMaterialGroupVm } from '/@/views/supplychain/sysmaterialgroup/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysMaterialGroupVm>>({
	vmModel: {
		Entity: {
			ID: null,
			Name: null,
			Code: null,
			ErpId: null,
			Description: null,
			State: '启用',
			CreateTime: null,
			UpdateTime: null,
			CreateBy: null,
			UpdateBy: null,
			Level: 0,
			Remark: null,
			ParentId: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
	AllSysMaterialGroups: [] as ISysSelectType[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		other.getSelectList('/api/SysMaterialGroup/GetSysMaterialGroups', [], false).then((x) => (stateData.AllSysMaterialGroups = x)),
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
